package com.jingdun.sport.api.controller;

import cn.hutool.core.util.RandomUtil;
import com.jingdun.sport.bean.model.QiNiuResponse;
import com.jingdun.sport.common.bean.Qiniu;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/sport/qiniu")
public class QiniuController {

    @Autowired
    private Qiniu qiniu;

    // 获取 七牛云的 token
    @GetMapping(value = "/getToken")
    public QiNiuResponse getToken() {
        QiNiuResponse qiNiuResponse = new QiNiuResponse();
        String accessKey = qiniu.getAccessKey();
        String secretKey = qiniu.getSecretKey();
        String bucket = qiniu.getBucket();
        long expireSeconds = 60 * 60 * 24;   //过期时间
        StringMap putPolicy = new StringMap();
        putPolicy.put("persistentOps", qiniu.getPolicyStr());
//        String fileName = UUID.randomUUID().toString().replaceAll("\\-", "");
//        putPolicy.put("saveKey", fileName);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
        qiNiuResponse.setKey(UUID.randomUUID().toString().replaceAll("\\-", ""));
        qiNiuResponse.setToken(upToken);
        return qiNiuResponse;
    }
}
